home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-25 | 9.2 KB | 332 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Ruler.cpp
- // Release Version: $ ODF 1 $
- //
- // Author: Laurent Delamare
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "ODFDraw.hpp"
-
- #ifndef CONSTANT_H
- #include "Constant.h"
- #endif
-
- #ifndef RULER_H
- #include "Ruler.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWRECSHP_H
- #include "FWRecShp.h" // FW_CRectShape
- #endif
-
- #ifndef FWTXTSHP_H
- #include "FWTxtShp.h" // FW_CTextShape
- #endif
-
- #ifndef FWLINSHP_H
- #include "FWLinShp.h" // FW_CLineShape
- #endif
-
- #ifndef FWCONTXT_H
- #include "FWContxt.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_ODFacet_xh
- #include <Facet.xh>
- #endif
-
- #ifndef SOM_ODWindow_xh
- #include <Window.xh>
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- //========================================================================================
- // Runtime Informations
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfdraw
- #endif
-
- FW_DEFINE_AUTO(CRuler)
- FW_DEFINE_CLASS_M1(CRuler, FW_CSuperView)
-
- const FW_ClassTypeConstant FW_LRuler = FW_TYPE_CONSTANT('r','u','l','r');
- FW_REGISTER_ARCHIVABLE_CLASS(FW_LRuler, CRuler, CRuler::Create, FW_CView::Read, CRuler::Destroy, FW_CView::Write)
-
- //========================================================================================
- // CLASS CRuler
- //========================================================================================
-
- const FW_Fixed kRulerExtent = FW_IntToFixed(1000);
-
- //----------------------------------------------------------------------------------------
- // CRuler::CRuler
- //----------------------------------------------------------------------------------------
-
- CRuler::CRuler(Environment* ev,
- FW_CSuperView* container,
- const FW_CRect& bounds,
- FW_EScrollingDirection scrollDir,
- FW_Fixed zoomFactor) :
- FW_CSuperView(ev,
- container,
- bounds,
- 0,
- (scrollDir & FW_kXScrolling) ?
- FW_CPoint(kRulerExtent, bounds.bottom - bounds.top)
- : FW_CPoint(bounds.right - bounds.left, kRulerExtent),
- scrollDir),
- fLineStyle(FW_kFixed0),
- fFont(FW_kTimes, FW_kPlain, FW_IntToFixed(10))
- {
- FW_ASSERT(scrollDir != FW_kNoScrolling);
-
- ZoomFactorChanged(ev, zoomFactor);
-
- // Bind the ruler to left or top egde of its superview by default
- FW_ViewBinding rulerBindings = (IsScrollingInY(ev)) ?
- FW_kFixedWidth + FW_kLeftBinding + FW_kTopBinding + FW_kBottomBinding :
- FW_kFixedHeight + FW_kLeftBinding + FW_kTopBinding + FW_kRightBinding;
- SetBindings(ev, rulerBindings);
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::CRuler
- //----------------------------------------------------------------------------------------
-
- CRuler::CRuler(Environment* ev) :
- FW_CSuperView(ev),
- fLineStyle(FW_kFixed0),
- fFont(FW_kTimes, FW_kPlain, FW_IntToFixed(10))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::~CRuler
- //----------------------------------------------------------------------------------------
-
- CRuler::~CRuler()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::ZoomFactorChanged
- //----------------------------------------------------------------------------------------
-
- void CRuler::ZoomFactorChanged(Environment* ev, FW_Fixed zoomFactor)
- {
- if (IsScrollingInY(ev))
- fFont.SetFontSize(FW_IntToFixed(10) / zoomFactor);
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::Draw
- //----------------------------------------------------------------------------------------
-
- void CRuler::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
- {
- FW_CViewContext vc(ev, this, odFacet, invalidShape);
-
- FW_CRect extent(FW_kZeroPoint, GetExtent(ev));
-
- extent.Intersection(FW_GetShapeBoundingBox(ev, invalidShape));
-
- FW_CRectShape::RenderRect(vc, extent, FW_kFill, FW_kWhiteEraseInk);
-
- if (IsScrollingInX(ev))
- RenderHorizontalRuler(vc, extent);
- else
- RenderVerticalRuler(vc, extent);
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::RenderGradation
- //----------------------------------------------------------------------------------------
-
- void CRuler::RenderGradation(FW_CViewContext& vc,
- short gradation,
- const FW_CPoint& pos)
- {
- FW_CString32 string;
- string.ReplaceAllAsSignedDecimalInteger(gradation);
-
- FW_CTextShape::RenderText(vc, string,
- FW_CPoint(pos.x - FW_kFixedPos1, pos.y - FW_kFixedPos1),
- fFont,
- FW_kTextAlignRight | FW_kTextAlignBottom);
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::RenderHorizontalRuler
- //----------------------------------------------------------------------------------------
-
- void CRuler::RenderHorizontalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
- {
- FW_CLineShape::RenderLine(vc,
- FW_CPoint(visibleRect.left, kRulerWidth - FW_kFixedPos1),
- FW_CPoint(visibleRect.right, kRulerWidth - FW_kFixedPos1),
- FW_kNormalInk, fLineStyle);
-
- short h = FW_FixedToInt(kRulerWidth);
- FW_Fixed full = FW_IntToFixed(2 * (h / 3));
- FW_Fixed half = FW_IntToFixed(h / 2);
- FW_Fixed third = FW_IntToFixed(h / 3);
-
- short short72 = 72;
-
- short gradation = FW_FixedToInt(visibleRect.left) / short72;
- FW_Fixed minX = FW_IntToFixed(short72 * gradation);
- FW_Fixed maxX = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.right + FW_IntToFixed(short72)) / short72));
-
- FW_Fixed fixed18 = FW_IntToFixed(18);
-
- short n = 0;
- FW_Fixed height = full;
- if (minX == FW_kFixed0)
- {
- minX = fixed18;
- n = 1;
- height = third;
- }
-
- FW_CPoint start(minX, kRulerWidth);
- FW_CPoint end(minX, kRulerWidth - height);
- for (FW_Fixed x = minX; x <= maxX; x += fixed18)
- {
- FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
- if (n == 0)
- RenderGradation(vc, gradation, start);
-
- n++;
- if (n == 4)
- {
- n = 0;
- gradation++;
- height = full;
- }
- else if (n == 2)
- height = half;
- else if (n == 1 || n == 3)
- height = third;
-
- start.x += fixed18;
- end.x += fixed18;
- end.y = kRulerWidth - height;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::RenderVerticalRuler
- //----------------------------------------------------------------------------------------
-
- void CRuler::RenderVerticalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
- {
- FW_CLineShape::RenderLine(vc,
- FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.top),
- FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.bottom),
- FW_kNormalInk, fLineStyle);
-
- short h = FW_FixedToInt(kRulerWidth);
- FW_Fixed full = FW_IntToFixed(2 * (h / 3));
- FW_Fixed half = FW_IntToFixed(h / 2);
- FW_Fixed third = FW_IntToFixed(h / 3);
-
- short short72 = 72;
-
- short gradation = FW_FixedToInt(visibleRect.top) / short72;
- FW_Fixed minY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.top) / short72));
- FW_Fixed maxY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.bottom + FW_IntToFixed(short72)) / short72));
-
- FW_Fixed fixed18 = FW_IntToFixed(18);
-
- short n = 0;
- FW_Fixed width = full;
- if (minY == FW_kFixed0)
- {
- minY = fixed18;
- n = 1;
- width = third;
- }
-
- FW_CPoint start(kRulerWidth, minY);
- FW_CPoint end(kRulerWidth - width, minY);
- for (FW_Fixed y = minY; y <= maxY; y += fixed18)
- {
- FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
- if (n == 0)
- RenderGradation(vc, gradation, start);
-
- n++;
- if (n == 4)
- {
- n = 0;
- gradation++;
- width = full;
- }
- else if (n == 2)
- width = half;
- else if (n == 1 || n == 3)
- width = third;
-
- start.y += fixed18;
- end.y += fixed18;
- end.x = kRulerWidth - width;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::Create
- //----------------------------------------------------------------------------------------
-
- void* CRuler::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
- {
- FW_UNUSED(stream);
- FW_UNUSED(type);
- FW_SOMEnvironment ev;
- return FW_NEW(CRuler, (ev));
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::Destroy
- //----------------------------------------------------------------------------------------
-
- void CRuler::Destroy(void* object, FW_ClassTypeConstant type)
- {
- FW_UNUSED(type);
- CRuler* self = (CRuler*) object;
- delete self;
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::Flatten
- //----------------------------------------------------------------------------------------
-
- void CRuler::Flatten(Environment* ev, FW_CWritableStream& archive) const
- {
- FW_CSuperView::Flatten(ev, archive);
- }
-
- //----------------------------------------------------------------------------------------
- // CRuler::InitializeFromStream
- //----------------------------------------------------------------------------------------
-
- void CRuler::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
- {
- FW_CSuperView::InitializeFromStream(ev, stream);
- }
-
-